#include "Protocol.h"

#include <sys/socket.h>

const string Protocol::START_SESSION_MSG = "START";
const string Protocol::END_SESSION_MSG = "END_CONV";
const string Protocol::TWIT_MSG = "TWIT";
const string Protocol::DIRECT_MESSAGE_MSG = "DM";
const string Protocol::PA_MESSAGE_MSG = "PA";
const string Protocol::FOLLOW_MSG = "FOLLOW";
const string Protocol::UNFOLLOW_MSG = "UNFOLLOW";
const string Protocol::CONNECT_MSG = "CONNECT";
const string Protocol::DISCONNECT_MSG = "DISCONNET";
const string Protocol::WHO_MSG = "WHO";
const string Protocol::ACK_MSG = "ACK";
const string Protocol::ERROR_MSG = "ERROR";
const string Protocol::END_LINE_MSG = "END";
const string Protocol::BYE_BYE_MSG = "BYE BYE";
const string Protocol::HELLO_MSG = "HELLO";
const string Protocol::WELCOME_MSG = "WELCOME";
const string Protocol::END_OF_LIST_MSG = "END_LIST";

const string Protocol::MSG_SEPARATOR = "@";

int Protocol::sendMessage(int socketFD,void* inputData,int size){
	//send data size
	send(socketFD, &size, sizeof(int), 0);

	//send actual data
	int numberOfSentBytesData = send(socketFD, inputData, size, 0);
	if(numberOfSentBytesData != size){
		return -1;
	}

	return numberOfSentBytesData;
}

int Protocol::readMessage(int socketFD,void*& outputData){
	//get data size
	int dataSize = 0;
	recv(socketFD, &dataSize, sizeof(int), 0);
	//allocate expected space
	outputData = new char[dataSize];
	int numberOfRecvBytesData = recv(socketFD, outputData, dataSize, 0);
	if(numberOfRecvBytesData != dataSize){
		return -1;
	}
	return dataSize;
}
